package jdly.water.api.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;

import jdly.water.api.model.PageModel;

public class PageUtil {

	public static  final String  DEFAULT_SORT_FIELD = "createtime";
	public static  final String  DEFAULT_SORT_NAME_COLUMNL = "CREATE_TIME";
	public static  final String  DEFAULT_SORT_TYPE = "DESC";
	public static  final String  PRIMARY_FIELD = "id";
	
	/**
	 * 获取分页对象
	 * 
	 * @param param
	 *            Layui表格参数
	 * @param where
	 *            条件查询json内容
	 * @return
	 */
	public static Pageable toPageable(PageModel param) {
		Sort sort = new Sort(Direction.DESC, DEFAULT_SORT_FIELD);
//		Pageable page = new PageRequest(param.getPage() - 1, param.getLimit(), sort);
		String sortname = param.getSortname();
		if (StringUtils.isNotEmpty(sortname)) {
			Order order = new Order(sortname);
			String ordertype = param.getOrdertype();
			if ("DESC".equals(ordertype)) {
				order.with(Direction.DESC);
			} else {
				order.with(Direction.ASC);
			}
			Order order2 = new Order(Direction.DESC, PRIMARY_FIELD);
			sort = new Sort(order,order2);
		}
		Pageable page = new PageRequest(param.getPage() - 1, param.getLimit(), sort);
		return page;
	}
}
